<template>
  <div class="goods-container">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <div style="margin-top: 15px;">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-input placeholder="请输入内容" v-model="userObj.query" class="iptBox">
              <el-button slot="append" icon="el-icon-search" @click="getGoods"></el-button>
            </el-input>
          </el-col>
          <el-col :span="8">
            <el-button type="primary" @click="addGoods">添加商品</el-button>
          </el-col>
        </el-row>
      </div>
      <!-- 表格 -->
      <el-table :data="tableData" border style="width: 100%;margin:20px 0">
        <el-table-column type="index" width="50" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="180"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格" width="180"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
        <el-table-column prop="add_time" label="创建时间"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)"
            ></el-button>
            <el-button
              icon="el-icon-delete"
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="userObj.pagenum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="userObj.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 编辑用户的表单对话框 -->
    <el-dialog title="修改商品信息" :visible.sync="dialogFormVisible">
      <el-form ref="formEditDialog" :model="formEdit" :rules="rulesFormEdit">
        <el-form-item label="商品名称" prop="goods_name" :label-width="formLabelWidth">
          <el-input v-model="formEdit.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price" :label-width="formLabelWidth">
          <el-input v-model="formEdit.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight" :label-width="formLabelWidth">
          <el-input v-model="formEdit.goods_weight"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editForm('formEditDialog')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>  
<script>
import { goodrs_api, usersEdit_api,goodsDelete_api } from "../../api/index.js";
export default {
  data() {
    return {
      // 初始数据
      tableData: [],
      /* 获取数据列表参数对象 */
      userObj: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      total: 1,
      /* 对话框数据 */
      dialogFormVisible: false,
      formEdit: {},
      formLabelWidth: "120px",
      //  表单校验规则
      rulesFormEdit: {
        goods_name: [
          { required: true, message: "输入姓名不能为空", trigger: "blur" },
        ],
        goods_price: [
          { required: true, message: "输入价格不能为空", trigger: "blur" },
        ],
        goods_weight: [
          { required: true, message: "输入重量不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getGoods();
  },
  methods: {
    // 分页
    handleSizeChange(val) {
      this.userObj.pagesize = val;
      this.getGoods();
    },
    handleCurrentChange(val) {
      this.userObj.pagenum = val;
      this.getGoods();
    },
    // 跳转
    addGoods() {
      this.$router.push("/add");
    },
    // 编辑按钮
    handleEdit(row) {
      this.formEdit = row;
      this.dialogFormVisible = true;
    },
    // 编辑功能实现,确认键
    editForm(formEditDialog) {
      this.$refs[formEditDialog].validate(async(valid) => {
        if (!valid) {
          return false;
        } else {
        　const { data: res } = await usersEdit_api(this.formEdit)
          this.dialogFormVisible = false
          this.getGoods()
        }
      });
    },
    // 删除按钮
    async handleDelete(row) {
      const {data:res} = await goodsDelete_api(row)
      this.getGoods()
    },

    // 获取初始数据
    async getGoods() {
      const { data: res } = await goodrs_api(this.userObj);
      this.tableData = res.data.goods;
      this.total = res.data.total;
    },
  },
};
</script>
<style scoped lang='less'>
.goods-container {
  .el-breadcrumb {
    margin-bottom: 20px;
  }
}
</style>